﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace QuickFramework.SqlSugarOrm
{
    public partial class Repository<T> : IRepository<T> where T : Shared.Contracts.EntityBase, new()
    {
        /// <summary>
        /// 数据库链接节点名称
        /// </summary>
        private string m_ConnectionStringName;
        /// <summary>
        /// 数据库链接名
        /// </summary>
        public string ConnectionStringName
        {
            get { return m_ConnectionStringName; }
        }
        /// <summary>
        /// 是否全部使用主库资源
        /// </summary>
        private bool m_IsMaster;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionStringName">数据库连接名</param>
        /// <param name="isMaster">是否全部使用主库资源</param>
        public Repository(string connectionStringName, bool isMaster = false)
        {
            m_ConnectionStringName = connectionStringName;
            m_IsMaster = isMaster;
        }
        /// <summary>
        /// 仓储内部使用的工作单元,使用当前数据库链接创建
        /// 每次获取都将获取到新的UnitOfWork,可以直接开启事务
        /// new UnitOfWork().CreateUoW(m_ConnectionStringName, m_IsMaster)
        /// </summary>
        public IUnitOfWork CreateUnitOfWork()
        {
            return new UnitOfWork().CreateUoW(m_ConnectionStringName);
        }
        /// <summary>
        /// ADO 对象
        /// </summary>
        public IAdo Ado(IUnitOfWork unitOfWork = null)
        {
            IUnitOfWork uow = unitOfWork;
            if (null == unitOfWork)
            {
                uow = this.CreateUnitOfWork();
            }
            return uow.SqlSugarClient.Ado;
        }
        /// <summary>
        /// SqlSugarClient 对象
        /// </summary>
        public SqlSugarClient SqlSugarClient(IUnitOfWork unitOfWork = null)
        {
            IUnitOfWork uow = unitOfWork;
            if (null == unitOfWork)
            {
                uow = this.CreateUnitOfWork();
            }
            return uow.SqlSugarClient;
        }
        /// <summary>
        /// SqlSugarClient 对象
        /// </summary>
        public SimpleClient<T> SimpleClient(IUnitOfWork unitOfWork = null)
        {
            IUnitOfWork uow = unitOfWork;
            if (null == unitOfWork)
            {
                uow = this.CreateUnitOfWork();
            }
            return uow.SqlSugarClient.GetSimpleClient<T>();
        }
        /// <summary>
        /// 只更新实体设定过值的栏位
        /// </summary>
        /// <param name="UpdateObj"></param>
        /// <param name="unitOfWork"></param>
        /// <returns></returns>
        public IUpdateable<T> Updateable4Set(T UpdateObj, IUnitOfWork unitOfWork = null)
        {
            IUnitOfWork uow = unitOfWork;
            if (null == unitOfWork)
            {
                uow = this.CreateUnitOfWork();
            }
            var result = uow.SqlSugarClient.Updateable<T>(UpdateObj).UpdateColumns(UpdateObj.PropertyChanges.ToArray());
            UpdateObj.ClearChanges();
            return result;
        }
    }
}
